package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("StandardController")
public class StandardController extends CmiEmaController {

	@RequestMapping("/listStandards")
	public ModelAndView listStandards(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = standardPaginationService.updatePagination(request, pagination);
		Set<Standard> standards = standardPaginationService.getPaginationStandards(pagination);

		mav.addObject("standards", standards);
		mav.addObject("entityName", "standard");
		mav.addObject("pagination", pagination);
		mav.setViewName("standard/listStandards.jsp");
		return mav;
	}

	public String listStandards() {
		return "redirect:/listStandards";
	}

	@RequestMapping("/viewStandard")
	public ModelAndView viewStandard(@RequestParam Integer standardId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("standard", standardDAO.findStandardByPrimaryKey(standardId));
		mav.setViewName("standard/viewStandard.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteStandards")
	public ModelAndView confirmDeleteStandards(
			@RequestParam(value = "deleteStandardsIds", required = true) Set<Integer> deleteStandardsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Standard> deleteStandards = standardDAO.findStandardsByIds(deleteStandardsIds);
		Set<Standard> referencedStandards = standardService.loadReferenceStandards(deleteStandards);
		boolean hasReferenced;
		if (referencedStandards.size() == 0) {
			mav.addObject("deleteEntitys", deleteStandards);
			hasReferenced = false;
		} else {
			deleteStandards.removeAll(referencedStandards);
			mav.addObject("referencedEntitys", referencedStandards);
			mav.addObject("deleteEntitys", deleteStandards);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Standard");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listStandards");
		return mav;
	}

	@RequestMapping("/deleteStandard")
	public String deleteStandard(@RequestParam Integer standardId) {
		Standard standard = standardDAO.findStandardByPrimaryKey(standardId);
		standardService.deleteStandard(standard);
		return "forward:/listStandards";
	}

	@RequestMapping("/confirmDeleteStandard")
	public ModelAndView confirmDeleteStandard(@RequestParam Integer standardId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("standard", standardDAO.findStandardByPrimaryKey(standardId));

		mav.setViewName("standard/deleteStandard.jsp");

		return mav;
	}

	@RequestMapping("/deleteStandards")
	public String deleteStandards(
			@RequestParam(value = "deleteStandardsId", required = true) Set<Integer> deleteStandardsId) {
		standardService.deleteStandards(deleteStandardsId);
		return "forward:/listStandards";
	}

	@RequestMapping("/editStandard")
	public ModelAndView editStandard(@RequestParam Integer standardId) {

		ModelAndView mav = new ModelAndView();

		Standard standard = standardDAO.findStandardByPrimaryKey(standardId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("standard", standard);

		// 把所有的引用standard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardService.loadStandardObjectReferencedSet(standard));

		// 将编辑页面standard添加到mav中
		mav.setViewName("standard/editStandard.jsp");
		return mav;
	}

	@RequestMapping("/newStandard")
	public ModelAndView newStandard() {
		ModelAndView mav = new ModelAndView();
		Standard standard = new Standard();

		mav.addObject("standard", standard);

		// 把所有的引用standard对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(standardService.loadStandardObjectReferencedSet(standard));

		mav.addObject("newFlag", true);
		mav.setViewName("standard/editStandard.jsp");

		return mav;
	}

	@RequestMapping("/saveStandard")
	public ModelAndView saveStandard(@ModelAttribute Standard standard) {

		ModelAndView mav = new ModelAndView();

		Standard replacedstandardTemp = standard.getReplacedstandard();
		Standard replacedstandard = replacedstandardTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(standardDAO.findStandardById(replacedstandardTemp.getId()));
		Standardtype standardtypeTemp = standard.getStandardtype();
		Standardtype standardtype = standardtypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(standardtypeDAO.findStandardtypeById(standardtypeTemp.getId()));
		Standardlevel standardlevelTemp = standard.getStandardlevel();
		Standardlevel standardlevel = standardlevelTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(standardlevelDAO.findStandardlevelById(standardlevelTemp.getId()));

		Standard existingStandard = CmiHibernateProxyUtil
				.deproxy(standardDAO.findStandardByPhysicalPrimaryKey(standard.getNumber()));

		standard.setReplacedstandard(replacedstandard);
		standard.setStandardtype(standardtype);
		standard.setStandardlevel(standardlevel);
		// 新建记录
		if (standard.getId() == null) {
			if (standard.equals(existingStandard)) {

				mav.addAllObjects(standardService.loadStandardClassReferencedSet(standard));
				// mav = editContinue(mav, standard);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standard/editStandard.jsp");
			} else {
				standardService.saveStandard(standard);
				mav.setViewName("forward:/listStandards");
			}
		}
		// 编辑记录
		else {
			Standard originStandard = CmiHibernateProxyUtil
					.deproxy(standardDAO.findStandardByPrimaryKey(standard.getId()));
			if (!standard.equals(originStandard) && standard.equals(existingStandard)) {
				mav.addAllObjects(standardService.loadStandardClassReferencedSet(standard));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("standard/editStandard.jsp");
			} else {
				standardService.saveStandard(standard);
				mav.setViewName("forward:/listStandards");
			}
		}
		return mav;
	}

	@RequestMapping("/standardController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
